package com.teamone.etl.systemsettings.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.teamone.etl.systemsettings.common.PackagingParameter;
import com.teamone.etl.systemsettings.controller.UserformController;
import com.teamone.etl.systemsettings.dao.UserformDao;
import com.teamone.etl.dto.LoginClass;
import com.teamone.etl.systemsettings.dto.UserformDTO;
import com.teamone.etl.systemsettings.entity.Userform;
import com.teamone.etl.systemsettings.service.UserformService;
import com.teamone.etl.systemsettings.utils.Md5Utils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;

/**
 * (Userform)表服务实现类
 *
 * @author makejava
 * @since 2021-01-21 09:58:20
 */
@Service("userformService")
@Transactional(rollbackFor = Exception.class)
public class UserformServiceImpl extends ServiceImpl<UserformDao, Userform> implements UserformService {




    @Autowired(required = false)
    private HttpServletRequest httpServletRequest;

    /**
     *单条件查询
     *
     * @param id 员工id
     * @return 单条数据
     */
    @Override
    public Userform selectOne(Integer id) {
        return baseMapper.selectOne(id);
    }

    /**
     *添加员工
     *
     * @param userform 员工信息
     * @return true false
     */
    @Override
    public Boolean createUserform(Userform userform) {

        Userform userform1 = baseMapper.selectUserformUsername(userform.getUsername());
        if(userform1!=null){
            return false;
        }
        //获取当前登录人
        Userform sessionUser = (Userform) httpServletRequest.getSession().getAttribute(UserformController.ADMIN_ID);
        //Md5加密
        userform.setPassword(Md5Utils.string2Md5(userform.getPassword()));
        userform.setCreateTime(new Date());
        userform.setUpdateTime(new Date());
        userform.setStatus(PackagingParameter.NUMBER.ONE.getCode());
        userform.setCreateMan(sessionUser.getId());

        baseMapper.createUserform(userform);
        return true;
    }


    /**
     * 修改密码
     *
     * @param id id
     * @param password 密码
     * @return true false
     */
    @Override
    public Boolean updateUserformPwd(Integer id, String password) {

        return baseMapper.updateUserformPwd(id,Md5Utils.string2Md5(password));
    }

    /**
     * 用户登录
     *
     * @return
     */
    @Override
    public Userform loginUser(LoginClass loginClass) {

        //共用同一条sql
        Userform userform = baseMapper.selectloginByUsername(loginClass.getUsername());

        if(userform!=null && userform.getPassword().equals(Md5Utils.string2Md5(loginClass.getPassword()))) {
            return userform;
        }
        return null;
    }


    /**
     * 修改部门
     *
     * @param id 员工id
     * @param did 部门编号
     * @param pid 职位编号
     * @return true false
     */
    @Override
    public Boolean updatePostByUser(Integer id, Integer did, Integer pid) {
        return baseMapper.updatePostByUser(id, did, pid);

    }

    /**
     * 删除员工
     *
     * @param id 员工id
     * @return true false
     */
    @Override
    public Boolean deleteUserById(Integer id) {
        return baseMapper.deleteUserById(id);
    }


    /**
     * 修改员工信息
     *
     * @param userformDTO 传输对象
     * @return true false
     */
    @Override
    public Boolean updateUser(UserformDTO userformDTO) {
        //查询用户名称是否已经存在
        Userform userform1 = baseMapper.selectUserformUsername(userformDTO.getUsername());

        if(userform1 != null){
            return false;
        }

        Userform userform = new Userform();
        userform.setId(userformDTO.getId());
        userform.setUsername(userformDTO.getUsername());
        userform.setName(userformDTO.getName());
        userform.setIphoneNumber(userformDTO.getIphoneNumber());
        userform.setDid(userformDTO.getDid());
        userform.setPid(userformDTO.getPid());
        userform.setUpdateTime(new Date());
        userform.setStatus(userformDTO.getStatus());
        baseMapper.updateUser(userform);

        return true;
    }

    /**
     * 通过用户名查询
     *
     * @param username 用户名
     * @return 用户信息
     */
    @Override
    public Userform selectUser(String username) {
        return baseMapper.selectUserformUsername(username);
    }


}